/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.spms.source.model;

import com.wisnu.spms.service.dao.DbBillingDao;
import com.wisnu.spms.service.dao.DbPaymentDao;
import com.wisnu.spms.service.dao.DbStudentDao;
import com.wisnu.spms.service.entity.DbBilling;
import com.wisnu.spms.service.entity.DbBillingtype;
import com.wisnu.spms.service.entity.DbCashlog;
import com.wisnu.spms.service.entity.DbPayment;
import com.wisnu.spms.service.entity.DbStatus;
import com.wisnu.spms.service.entity.DbStudent;
import com.wisnu.spms.service.util.SpringUtil;
import com.wisnu.spms.source.Employee;
import com.wisnu.spms.source.listener.PanelPaymentListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Wisnu Wardoyo
 */
public class PanelPaymentModel {

    private Integer id;
    private DbStudent student;
    private List<DbBilling> billing = new ArrayList<>();
    private List<DbPayment> payment = new ArrayList<>();
    private PanelPaymentListener listener;

    public PanelPaymentModel(PanelPaymentListener listener) {
        this.listener = listener;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public DbStudent getStudent() {
        return student;
    }

    public void setStudent(DbStudent student) {
        this.student = student;
        onChangeStudent(this);
    }

    public List<DbBilling> getBilling() {
        return billing;
    }

    public void setBilling(List<DbBilling> billing) {
        this.billing = billing;
        onChangeBillingList(this);
    }

    public void setPayment(List<DbPayment> payment) {
        this.payment = payment;
        onChangeTableData(this);
    }

    public List<DbPayment> getPayment() {
        return payment;
    }

    public void loadPage() throws Exception {
        DbStudentDao studentDao = SpringUtil.getDbStudentDao();
        setStudent(studentDao.getStudentById(id));
        DbBillingDao billingDao = SpringUtil.getBillingDao();
        setBilling(billingDao.getDbBillingById(student));
    }

    public void recordPayment(Integer billingOrder) throws Exception {
        DbPayment payment = new DbPayment();
        payment.setDbBilling(billing.get(billingOrder));
        payment.setPaymentDate(new Date());

        DbCashlog cashlog = new DbCashlog(Employee.getEmployee(), payment);

        DbPaymentDao dao = SpringUtil.getDbPaymentDao();
        dao.insertPayment(payment, cashlog);
    }

    public void billingChecker(Integer billingOrder) throws Exception {
        DbBilling activeBilling = this.billing.get(billingOrder);
        DbPaymentDao paymentDao = SpringUtil.getDbPaymentDao();
        DbBillingDao billingDao = SpringUtil.getBillingDao();

        this.payment = paymentDao.getPaymentByBilling(activeBilling);
        if (payment.size() == activeBilling.getDbBillingtype().getBillingPeriod()) {
            activeBilling.setDbStatus(new DbStatus(2, "Lunas"));
            billingDao.updateBilling(activeBilling);
            loadPage();
        }
    }

    public void setBillingAmount(int order) throws Exception {
        setBillingAmount(this.billing.get(order).getDbBillingtype());
        DbPaymentDao dao = SpringUtil.getDbPaymentDao();
        setPayment(dao.getPaymentByBilling(this.billing.get(order)));
    }

    public Integer checkStudent(Integer id) throws Exception {
        DbStudentDao dao = SpringUtil.getDbStudentDao();
        return dao.getStudentById(id) == null ? 0 : 1;
    }

    protected void onChangeStudent(PanelPaymentModel model) {
        listener.onChangeStudent(model);
    }

    protected void onChangeBillingList(PanelPaymentModel model) {
        listener.onChangeBillingList(model);
    }

    protected void setBillingAmount(DbBillingtype billingType) {
        listener.setBillingAmount(billingType);
    }

    protected void onChangeTableData(PanelPaymentModel model) {
        listener.onChangeTableData(model);
    }

}
